<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateUserSecretsTable extends Migration
{
    /**
     * 数据表名称
     * @var string
     */
    private $tableName = 'user_secrets';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建数据表
        Schema::create($this->tableName, function (Blueprint $table) {
            // 数据表类型
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

            // 数据表字段
            $table->bigIncrements('secret_id')->comment('密码主键');
            $table->bigInteger('user_id', false, true)->nullable()->comment('用户主键');
            $table->string('mode', 20)->comment('密钥模式：secret:登陆密码 wx_unionid:微信唯一标识 wx_app:微信APP标识 wx_xcx:微信小程序标识 wx_gzh:微信公众号标识');
            $table->string('secret', 200)->comment('身份密钥');
            $table->string('unionid', 200)->nullable()->comment('微信身份标识');
            $table->string('cookie', 300)->nullable()->comment('cookie');
            $table->string('session', 300)->nullable()->comment('会话密钥');
            $table->string('current_token', 500)->nullable()->comment('当前Token字符串');
            $table->tinyInteger('is_keep')->default(0)->comment('是否关注 0:否 1:是');
            $table->dateTime('created_at')->comment('数据创建时间');
            $table->timestamp('updated_at')->comment('最后更新时间')->default(DB::raw('CURRENT_TIMESTAMP'));

            // 数据表索引
            $table->index('user_id');
            $table->unique(['user_id', 'mode']);
        });
        // 设置数据表备注信息
        DB::statement("ALTER TABLE `".env('DB_PREFIX')."{$this->tableName}` COMMENT'用户密钥表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
